﻿/*----------------------------------------------------------------
//  <copyright file="ClientHttpCryptoHandler.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2024-03-04 14:39</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud.Http
{
    #region "HttpClient客户端加密通信处理器"
    /// <summary>
    /// HttpClient客户端加密通信处理器
    /// </summary>
    public class ClientHttpCryptoHandler : DelegatingHandler
    {
        //字段
        private readonly IClientHttpCrypto _clientHttpCrypto;

        #region "构造函数"
        #region "初始化一个HttpClient客户端加密通信处理器的新实例"
        /// <summary>
        /// 初始化一个HttpClient客户端加密通信处理器 <see cref="ClientHttpCryptoHandler"/> 的新实例
        /// </summary>
        /// <param name="clientHttpCrypto"></param>
        public ClientHttpCryptoHandler(IClientHttpCrypto clientHttpCrypto)
        {
            _clientHttpCrypto = clientHttpCrypto;
        }
        #endregion

        #endregion

        #region "方法"
        #region "将HTTP请求发送到内部处理程序以作为异步操作发送到服务器"
        /// <summary>
        /// 将HTTP请求发送到内部处理程序以作为异步操作发送到服务器
        /// Sends an HTTP request to the inner handler to send to the server as an asynchronous operation.
        /// </summary>
        /// <param name="request">要发送到服务器的HTTP请求消息</param>
        /// <param name="cancellationToken">取消操作的取消令牌</param>
        /// <returns>表示异步操作的任务对象</returns>
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            request = await _clientHttpCrypto.EncryptRequest(request);
            HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
            response = await _clientHttpCrypto.DecryptResponse(response);
            return response;
        }
        #endregion

        #endregion

    }
    #endregion

}
